package com.xcx.commonuntils.until;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;


public class CheckNOtNullUtils {


    public static boolean checkObjAllFieldsIsNull(Object object) {
        boolean flag = false;
        for(Field f : object.getClass().getDeclaredFields()){
            f.setAccessible(true);

            try {
                if(f.get(object) == null || f.get(object).equals("")){

                    flag = true;
                    return flag;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return flag;

    }

    public static String[] getNullPropert(Object source){
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);

    }









}
